<?php
// Main menu
$al_menu[] = array('Dashboard');
$al_menu[] = array('Write');
$al_menu[] = array('Manage');
//$al_menu[] = array('Links', 'links.php');
//$al_menu[] = array('Themes', 'themes.php');

$al_menu[] = array('Users');
$al_menu[] = array('Maintenance');
$al_menu[] = array('Options');
$al_menu[] = array('Plugins');
//$al_menu[] = array('Data Management', 'backup.php');

$al_menu = apply_filters('admin_menu', $al_menu);

// sub menu Dashboard
$al_sub_menu['Dashboard'][0] = array('Dashboard', 'dashboard', 'action'=>array('read'));
$al_sub_menu['Dashboard'][1] = array('Disk Space', 'disk_space', 'action'=>array('read'));

// sub menu Write
$al_sub_menu['Write'][0] = array('Write Post', 'post_new', 'action'=>array('read and manage'));

//$al_sub_menu['Write'][1] = array('Drafts', 'drafts');

// sub menu Manage
$al_sub_menu['Manage'][0] = array('Diagram', 'diagram', 'action'=>array('read', 'read and manage'));
$al_sub_menu['Manage'][1] = array('Posts', 'posts', 'action'=>array('read', 'read and manage'));
$al_sub_menu['Manage'][2] = array('Comments', 'comments', 'action'=>array('read', 'read and manage'));
$al_sub_menu['Manage'][3] = array('Diagram Manage', 'diagram_manage', 'action'=>array('read', 'read and manage'));

$al_sub_menu['Manage'][] = array('', 'manage');
$al_sub_menu['Manage'][] = array('', 'diagram_new');
$al_sub_menu['Manage'][] = array('', 'page_edit');

$al_sub_menu['Manage'][] = array('', 'edit_post');

// sub menu Links
//$al_sub_menu['Links'][0] = array('List of Links', 'links.php');
//$al_sub_menu['Links'][1] = array('Keyword link', 'keywordlink.php');

// plugins
$al_sub_menu['Plugins'][0] = array('Plugins', 'plugins', 'action'=>array('read and manage'));


$al_sub_menu['Users'][0] = array('Users', 'users', 'action'=>array('read', 'read and manage'));
$al_sub_menu['Users'][1] = array('Groups', 'groups', 'action'=>array('read', 'read and manage'));
$al_sub_menu['Users'][2] = array('Your Profile', 'profile', 'action'=>array('read and manage'));
$al_sub_menu['Users'][] = array('', 'user_edit');
$al_sub_menu['Users'][] = array('', 'user_create');
$al_sub_menu['Users'][] = array('', 'group_edit');
$al_sub_menu['Users'][] = array('', 'group_new');

// Options

$al_sub_menu['Options'][0] = array('General', 'options_general', 'action'=>array('read and manage'));
$al_sub_menu['Options'][1] = array('Image setting', 'options_image', 'action'=>array('read and manage'));
$al_sub_menu['Options'][2] = array('Permalinks', 'options_permalink', 'action'=>array('read and manage'));
$al_sub_menu['Options'][3] = array('Cache', 'options_cache', 'action'=>array('read and manage'));

// Maintenance 

$al_sub_menu['Maintenance'][] = array('Database Backup', 'data_backup', 'action'=>array('read and manage'));
$al_sub_menu['Maintenance'][] = array('Database Restore', 'data_restore', 'action'=>array('read and manage'));
$al_sub_menu['Maintenance'][] = array('Update Cache', 'update_cache', 'action'=>array('read and manage'));
//$al_sub_menu['Maintenance'][] = array('Themes', 'themes', 'action'=>array('read and manage'));
/*
$al_sub_menu['Maintenance'][] = array('Filter', 'maintenance');
$al_sub_menu['Maintenance'][] = array('Admin log', 'adminlog');
$al_sub_menu['Maintenance'][] = array('User logs', 'userlog');
$al_sub_menu['Maintenance'][] = array('Error log', 'errorlog');
*/

// sub menu Data Management
/*
$al_sub_menu['Data Management'][0] = array('Data backup', 'backup.php');
$al_sub_menu['Data Management'][1] = array('Data correction', 'correction.php');
$al_sub_menu['Data Management'][2] = array('Optimize database', 'optimize.php');
$al_sub_menu['Data Management'][3] = array('Restore data', 'restore.php');
*/
if (!isset($self_sub_menu)) {
    $self_sub_menu = null;
}
$al_sub_menu = apply_filters('admin_sub_menu', $al_sub_menu);

// Remove menus that have no accessible submenus and require privs that the user does not have.
// Run re-parent loop again.

foreach( $al_menu as $id => $data ) {
    $al_menu_display = false;
/*
    if(!current_user_can($data[0], 'read')) {
        unset($al_sub_menu[$data[0]]);
        unset($al_menu[$id]);
        continue;
    }
*/
    if(isset($al_sub_menu[$data['0']]) && !empty($al_sub_menu[$data['0']])) {
        foreach($al_sub_menu[$data['0']] as $sub_id => $sub_data) {
            if (!current_user_can($sub_data[1], 'read')) {
                unset($al_sub_menu[$data[0]][$sub_id]);
            }else {
                if(!$al_menu_display) {
                    $al_menu[$id][1] = $sub_data[1];
                }
                $al_menu_display = true;
            }
        }
    }

    if(!$al_menu_display) {
        unset($al_menu[$id]);
    }
}

unset($id);
unset($sub_id);
unset($data);
unset($sub_data);
unset($al_menu_display);
?>

<div id="main-menu">
    <ul>
<?php
$self = $this->action;
$this->page_title = $self;
foreach($al_menu as $item) {
    $class = '';
    if($al_sub_menu[$item[0]]) {
        foreach($al_sub_menu[$item[0]] as $sub_item) {
            if($sub_item[1] == $self) {
                $class = "class=\"selected\"";
                $self_sub_menu = $al_sub_menu[$item[0]];
                $if_sub_item = $sub_item[0] == '' ? 0 : 1;
            }
        }
    }
    if($item[1] == $self) {
        $class = "class=\"selected\"";
    }
?>
    <li <?=$class?>><?php echo $html->admin_link(__($item[0]), '/' . $item[1]); ?></li>
<?php
}
?>                    
    </ul>
  <div class="clear"></div>
</div>

<?php
if( $self_sub_menu && $if_sub_item ) {
?>
<div id="sub-menu-box">
    <ul id="sub-menu">
<?php
    $i = 1;
    foreach($self_sub_menu as $item) {
        $class = '';
        $drafts_count = '';
        if( $item[1] == $self )
            $class = 'class="selected" ';
        if( $i == 1 )
            $class .= ' id="sub_menu_the_firstli"';
        $i++;
        if($item[0] == 'Drafts') {
            $drafts_count = current_user_draft_count();
            if(!empty($drafts_count)) {
                $drafts_count = '<span id="drafts_count" >(' . $drafts_count . ')</span>';
            }else {
                $drafts_count = '<span id="drafts_count" ></span>';
            }
        }
?>
    <li <?=$class?>><?php echo $html->admin_link(__($item[0]).$drafts_count, '/' . $item[1]); ?></li>
<?php
    }
?>
    </ul>
</div>
<?php
}
unset($self);
unset($class);
unset($item);
unset($drafts_count);
unset($item);
unset($item);
unset($item);
unset($if_sub_item);
unset($al_menu);
unset($sub_item);
unset($self_sub_menu);
if (!defined('USE_SUB_MENU')) {
    unset($al_sub_menu);
}
?>
